<template>
  <div>
    <el-dialog
      title="推荐"
      :visible.sync="dialogVisible"
      width="600px"
      @closed="resetForm()"
      :close-on-click-modal="false"
    >
      <div>
        <el-form ref="form" :model="form" label-width="80px">
          <el-form-item label="推荐序号">
            <el-input-number
              v-model="form.orderBy"
              :min="1"
              :max="100"
              size="medium"
              label="推荐序号"
            ></el-input-number>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="custom" v-preventReClick @click="submitForm()"
          >提 交</el-button
        >
        <el-button @click="dialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { sysCourseUpdate } from "@/api/admin/courseLibrary/sysCourse.js";
import Bus from "@/utils/bus.js";
export default {
  data() {
    return {
      dialogVisible: false,
      form: {
        id: "",
        recommended: "0",
        orderBy: "10",
      },
    };
  },
  mounted() {
    Bus.$on("showDialogCourseRecommend", (v, type) => {
      this.form.id = v.id;
      this.form.recommended = type;
      if (type == 0) {
        //取消推荐
        this.form.orderBy = 10;
        this.Recommend();
      } else {
        //推荐
        this.form.orderBy = 10;

        this.dialogVisible = true;
      }
    });
  },
  beforeDestroy() {
    Bus.$off("showDialogCourseRecommend");
  },
  methods: {
    //显示弹窗
    submitForm() {
      this.Recommend();
    },
    Recommend() {
      sysCourseUpdate(this.form).then((res) => {
        Bus.$emit("refreshCourseList");
        this.dialogVisible = false;
      });
    },
    //重置表单
    resetForm() {
      this.form.id = "";
      this.form.orderBy = "10";
      this.form.recommended = "0";
    },
  },
};
</script>